package com.example.he.p0305_event_btn;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

//实现事件监听器接口
public class EventBtnActivity extends Activity implements OnClickListener {
    //点击Button
    private Button clickBtn;
    //文字显示
    private TextView showTxt;
    //单击计数
    static int click_count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_btn);
        //初始化组件
        showTxt = (TextView) findViewById(R.id.showTxt);
        clickBtn = (Button) findViewById(R.id.clickBtn);
        //单击计数初值
        click_count=0;
        // 直接使用Activity作为事件监听器
        clickBtn.setOnClickListener(this);
        showTxt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // 实现事件处理方法
        showTxt.setText("发生单击！No. " + (++click_count));
        //检测单击发生在哪里并提示
        switch (v.getId()) {
            case R.id.clickBtn:
                Toast.makeText(getApplicationContext(), "按钮被单击", Toast.LENGTH_SHORT).show();
                break;
            case R.id.showTxt:
                Toast.makeText(getApplicationContext(), "文本被单击", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getApplicationContext(), "其他被单击", Toast.LENGTH_SHORT).show();
        }
    }
}